<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>CMSIS-RTOS RTX: Using CMSIS-RTOS RTX</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="cmsis.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="stylsheetf" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 46px;">
  <td id="projectlogo"><img alt="Logo" src="CMSIS_Logo_Final.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">CMSIS-RTOS RTX
   &#160;<span id="projectnumber">Version 4.74</span>
   </div>
   <div id="projectbrief">CMSIS-RTOS RTX: Real-Time Operating System for Cortex-M processor-based devices</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<div id="CMSISnav" class="tabs1">
    <ul class="tablist">
      <li class="current"><a href="./index.html"><span>RTX</span></a></li>
    </ul>
</div>
<!-- Generated by Doxygen 1.8.2 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Usage&#160;and&#160;Description</span></a></li>
      <li><a href="modules.html"><span>Reference</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('_using.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Using CMSIS-RTOS RTX </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This section explains how to create a simple application with the CMSIS-RTOS RTX kernel.</p>
<h1><a class="anchor" id="Template"></a>
Template Project</h1>
<p>The directory <b>Templates</b> contains CMSIS-RTOS RTX templates for every supported compiler and every supported Cortex-M processor.</p>
<table  class="cmtable" summary="CMSIS-RTOS RTX Library Files">
<tr>
<th>File or Directory </th><th>Content  </th></tr>
<tr>
<td>main.c </td><td>Template for a CMSIS-RTOS RTX main thread function.  </td></tr>
<tr>
<td>RTX_Conf_CM.c </td><td>Configuration File for CMSIS-RTOS RTX. Refer to <a class="el" href="_configure.html">Configuration of CMSIS-RTOS RTX</a> for more information.  </td></tr>
<tr>
<td>.\CM0 </td><td>Template project files for Cortex-M0 and Cortex-M1 processor.  </td></tr>
<tr>
<td>.\CM3 </td><td>Template project files for Cortex-M3 and Cortex-M4 without FPU processor.  </td></tr>
<tr>
<td>.\CM4 </td><td>Template project files for Cortex-M4 with FPU processor.  </td></tr>
<tr>
<td>.\CMx\ARM </td><td>Template project files for ARMCC Compiler.  </td></tr>
<tr>
<td>.\CMx\G++ </td><td>Template project files for CodeSoucery G++ Compiler.  </td></tr>
<tr>
<td>.\CMx\GCC </td><td>Template project files for GCC Compiler.  </td></tr>
<tr>
<td>.\CMx\IAR </td><td>Template project files for IAR Compiler.  </td></tr>
</table>
<p>Each template project file shows the basic configuration and the usage of the CMSIS-RTOS RTX Library and Configuration File. You may directly load this project templates with Keil MDK or IAR EW-ARM.</p>
<div class="image">
<img src="UV4_Template.png" alt="UV4_Template.png"/>
<div class="caption">
CMSIS-RTOS RTX Template loaded with Keil MDK-ARM.</div></div>
<p> The file main.c is the very minimal valid CMSIS-RTOS file and contains simply just a 'main' function that is executed as a thread.</p>
<h1><a class="anchor" id="Example1"></a>
Example Project .\Examples\RTX_ex1</h1>
<p>The example project provided in <b></b>.\Examples\RTX_ex1 shows the usage of:</p>
<ul>
<li><a class="el" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gac59b5713cb083702dce759c73fd90dff">osThreadCreate</a> to start an additional thread.</li>
<li><a class="el" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7">osThreadGetId</a> to obtain the thread ID of the 'main' thread.</li>
<li><a class="el" href="group___c_m_s_i_s___r_t_o_s___signal_mgmt.html#ga38860acda96df47da6923348d96fc4c9">osSignalWait</a> and <a class="el" href="group___c_m_s_i_s___r_t_o_s___signal_mgmt.html#ga3de2730654589d6c3559c4b9e2825553">osSignalSet</a> to synchronize thread execution.</li>
<li><a class="el" href="group___c_m_s_i_s___r_t_o_s___wait.html#ga02e19d5e723bfb06ba9324d625162255">osDelay</a> to generate time delays during thread execution.</li>
</ul>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os_8h.html">cmsis_os.h</a>&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Forward reference */</span></div>
<div class="line"><span class="keywordtype">void</span> threadX (<span class="keywordtype">void</span> <span class="keyword">const</span> *argument);</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Thread IDs */</span></div>
<div class="line"><a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f" title="Thread ID identifies the thread (pointer to a thread control block).">osThreadId</a> main_id;</div>
<div class="line"><a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f" title="Thread ID identifies the thread (pointer to a thread control block).">osThreadId</a> threadX_id;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Thread definitions */</span></div>
<div class="line"><a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaee93d929beb350f16e5cc7fa602e229f" title="Create a Thread Definition with function, priority, and stack requirements.">osThreadDef</a>(threadX, <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1" title="priority: normal (default)">osPriorityNormal</a>, 1, 0);</div>
<div class="line"> </div>
<div class="line"><span class="comment">/*----------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">     Thread X</span></div>
<div class="line"><span class="comment"> *---------------------------------------------------------------------------*/</span></div>
<div class="line"><span class="keywordtype">void</span> threadX (<span class="keywordtype">void</span> <span class="keyword">const</span> *argument) {</div>
<div class="line">  <span class="keywordflow">for</span> (;;) {</div>
<div class="line">    <span class="comment">/* Wait for completion of do-this */</span></div>
<div class="line">    <a class="code" href="group___c_m_s_i_s___r_t_o_s___signal_mgmt.html#ga38860acda96df47da6923348d96fc4c9" title="Wait for one or more Signal Flags to become signaled for the current RUNNING thread.">osSignalWait</a>(0x0004, <a class="code" href="cmsis__os_8h.html#a9eb9a7a797a42e4b55eb171ecc609ddb" title="Timeout value.">osWaitForever</a>); <span class="comment">/* do-that */</span></div>
<div class="line">    <span class="comment">/* Pause for 20 ms until signaling event to main thread */</span></div>
<div class="line">    <a class="code" href="group___c_m_s_i_s___r_t_o_s___wait.html#ga02e19d5e723bfb06ba9324d625162255" title="Wait for Timeout (Time Delay).">osDelay</a>(20);</div>
<div class="line">    <span class="comment">/* Indicate to main thread completion of do-that */</span></div>
<div class="line">    <a class="code" href="group___c_m_s_i_s___r_t_o_s___signal_mgmt.html#ga3de2730654589d6c3559c4b9e2825553" title="Set the specified Signal Flags of an active thread.">osSignalSet</a>(main_id, 0x0004);</div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">/*----------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">     Main Thread</span></div>
<div class="line"><span class="comment"> *---------------------------------------------------------------------------*/</span></div>
<div class="line"><span class="keywordtype">int</span> main (<span class="keywordtype">void</span>) {</div>
<div class="line"></div>
<div class="line">  <span class="comment">/* Get main thread ID */</span></div>
<div class="line">  main_id = <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gab1df2a28925862ef8f9cf4e1c995c5a7" title="Return the thread ID of the current running thread.">osThreadGetId</a>();</div>
<div class="line">  <span class="comment">/* Create thread X */</span></div>
<div class="line">  threadX_id = <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gac59b5713cb083702dce759c73fd90dff" title="Create a thread and add it to Active Threads and set it to state READY.">osThreadCreate</a>(<a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453" title="Access a Thread definition.">osThread</a>(threadX), NULL);</div>
<div class="line">  <span class="keywordflow">for</span> (;;) {    <span class="comment">/* do-this */</span></div>
<div class="line">    <span class="comment">/* Indicate to thread X completion of do-this */</span></div>
<div class="line">    <a class="code" href="group___c_m_s_i_s___r_t_o_s___signal_mgmt.html#ga3de2730654589d6c3559c4b9e2825553" title="Set the specified Signal Flags of an active thread.">osSignalSet</a>(threadX_id, 0x0004);</div>
<div class="line">    <span class="comment">/* Wait for completion of do-that */</span></div>
<div class="line">    <a class="code" href="group___c_m_s_i_s___r_t_o_s___signal_mgmt.html#ga38860acda96df47da6923348d96fc4c9" title="Wait for one or more Signal Flags to become signaled for the current RUNNING thread.">osSignalWait</a>(0x0004, <a class="code" href="cmsis__os_8h.html#a9eb9a7a797a42e4b55eb171ecc609ddb" title="Timeout value.">osWaitForever</a>);</div>
<div class="line">    <span class="comment">/* Wait now for 50 ms */</span></div>
<div class="line">    <a class="code" href="group___c_m_s_i_s___r_t_o_s___wait.html#ga02e19d5e723bfb06ba9324d625162255" title="Wait for Timeout (Time Delay).">osDelay</a>(50);</div>
<div class="line">  }</div>
<div class="line">}</div>
</div><!-- fragment --><h1><a class="anchor" id="Example2"></a>
Example Project .\Examples\RTX_ex2</h1>
<p>The example project provided in <b></b>.\Examples\RTX_ex2 shows in addition to the features demonstrated in RTX_ex1 the usage of:</p>
<ul>
<li><a class="el" href="group___c_m_s_i_s___r_t_o_s___kernel_ctrl.html#ga53d078a801022e202e8115c083ece68e">osKernelInitialize</a> which stops thread switching of the CMSIS-RTOS RTX to run an initialization sequence.</li>
<li><a class="el" href="group___c_m_s_i_s___r_t_o_s___kernel_ctrl.html#gaab668ffd2ea76bb0a77ab0ab385eaef2">osKernelStart</a> which starts thread switching and resumes the operation of the CMSIS-RTOS RTX thread scheduler.</li>
<li><a class="el" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaf13a667493c5d629a90c13e113b99233">osThreadYield</a> which provides a method for co-operative thread switching.</li>
</ul>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="cmsis__os_8h.html">cmsis_os.h</a>&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">volatile</span> uint16_t counter;            <span class="comment">/* counter for main thread             */</span></div>
<div class="line"><span class="keyword">volatile</span> uint16_t counter1;           <span class="comment">/* counter for thread 1                */</span></div>
<div class="line"><span class="keyword">volatile</span> uint16_t counter2;           <span class="comment">/* counter for thread 2                */</span></div>
<div class="line"><span class="keyword">volatile</span> uint16_t counter3;           <span class="comment">/* counter for thread 3                */</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Thread IDs */</span></div>
<div class="line"><a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f" title="Thread ID identifies the thread (pointer to a thread control block).">osThreadId</a> thread1_id;</div>
<div class="line"><a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f" title="Thread ID identifies the thread (pointer to a thread control block).">osThreadId</a> thread2_id;</div>
<div class="line"><a class="code" href="cmsis__os_8h.html#adfeb153a84a81309e2d958268197617f" title="Thread ID identifies the thread (pointer to a thread control block).">osThreadId</a> thread3_id;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Forward reference */</span></div>
<div class="line"><span class="keywordtype">void</span> job1 (<span class="keywordtype">void</span> <span class="keyword">const</span> *argument);</div>
<div class="line"><span class="keywordtype">void</span> job2 (<span class="keywordtype">void</span> <span class="keyword">const</span> *argument);</div>
<div class="line"><span class="keywordtype">void</span> job3 (<span class="keywordtype">void</span> <span class="keyword">const</span> *argument);</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Thread definitions */</span></div>
<div class="line"><a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaee93d929beb350f16e5cc7fa602e229f" title="Create a Thread Definition with function, priority, and stack requirements.">osThreadDef</a>(job1, <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa17b36cd9cd38652c2bc6d4803990674b" title="priority: above normal">osPriorityAboveNormal</a>, 1, 0);</div>
<div class="line"><a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaee93d929beb350f16e5cc7fa602e229f" title="Create a Thread Definition with function, priority, and stack requirements.">osThreadDef</a>(job2, <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1" title="priority: normal (default)">osPriorityNormal</a>, 1, 0);</div>
<div class="line"><a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaee93d929beb350f16e5cc7fa602e229f" title="Create a Thread Definition with function, priority, and stack requirements.">osThreadDef</a>(job3, <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#ga7f2b42f1983b9107775ec2a1c69a849aa45a2895ad30c79fb97de18cac7cc19f1" title="priority: normal (default)">osPriorityNormal</a>, 1, 0);</div>
<div class="line"> </div>
<div class="line"><span class="comment">/*----------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">     Thread 1: &#39;job1&#39;</span></div>
<div class="line"><span class="comment"> *---------------------------------------------------------------------------*/</span></div>
<div class="line"><span class="keywordtype">void</span> job1 (<span class="keywordtype">void</span> <span class="keyword">const</span> *argument) {    <span class="comment">/* higher priority to preempt job2     */</span></div>
<div class="line">  <span class="keywordflow">while</span> (1) {                         <span class="comment">/* endless loop                        */</span></div>
<div class="line">    counter1++;                       <span class="comment">/* increment counter 1                 */</span></div>
<div class="line">    <a class="code" href="group___c_m_s_i_s___r_t_o_s___wait.html#ga02e19d5e723bfb06ba9324d625162255" title="Wait for Timeout (Time Delay).">osDelay</a>(10);                      <span class="comment">/* wait for timeout: 10ms              */</span></div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">/*----------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">     Thread 2: &#39;job2&#39;</span></div>
<div class="line"><span class="comment"> *---------------------------------------------------------------------------*/</span></div>
<div class="line"><span class="keywordtype">void</span> job2 (<span class="keywordtype">void</span> <span class="keyword">const</span> *argument) {</div>
<div class="line">  <span class="keywordflow">while</span> (1)  {                        <span class="comment">/* endless loop                        */</span></div>
<div class="line">    counter2++;                       <span class="comment">/* increment counter 2                 */</span></div>
<div class="line">    <span class="keywordflow">if</span> (counter2 == 0) {              <span class="comment">/* signal overflow of counter 2        */</span></div>
<div class="line">      <a class="code" href="group___c_m_s_i_s___r_t_o_s___signal_mgmt.html#ga3de2730654589d6c3559c4b9e2825553" title="Set the specified Signal Flags of an active thread.">osSignalSet</a>(thread3_id, 0x0001);<span class="comment">/* to thread 3                         */</span></div>
<div class="line">      <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaf13a667493c5d629a90c13e113b99233" title="Pass control to next thread that is in state READY.">osThreadYield</a>();</div>
<div class="line">    }</div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">/*----------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">     Thread 3: &#39;job3&#39;</span></div>
<div class="line"><span class="comment"> *---------------------------------------------------------------------------*/</span></div>
<div class="line"><span class="keywordtype">void</span> job3 (<span class="keywordtype">void</span> <span class="keyword">const</span> *argument) {</div>
<div class="line">  <span class="keywordflow">while</span> (1) {                         <span class="comment">/* endless loop                        */</span></div>
<div class="line">    <a class="code" href="group___c_m_s_i_s___r_t_o_s___signal_mgmt.html#ga38860acda96df47da6923348d96fc4c9" title="Wait for one or more Signal Flags to become signaled for the current RUNNING thread.">osSignalWait</a>(0x0001, <a class="code" href="cmsis__os_8h.html#a9eb9a7a797a42e4b55eb171ecc609ddb" title="Timeout value.">osWaitForever</a>);  <span class="comment">/* wait for signal event           */</span></div>
<div class="line">    counter3++;                       <span class="comment">/* process overflow from counter 2     */</span></div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">/*----------------------------------------------------------------------------</span></div>
<div class="line"><span class="comment">     Main Thread</span></div>
<div class="line"><span class="comment"> *---------------------------------------------------------------------------*/</span></div>
<div class="line"><span class="keywordtype">int</span> main (<span class="keywordtype">void</span>) {                     <span class="comment">/* program execution starts here       */</span></div>
<div class="line"> </div>
<div class="line">  <span class="comment">/* Set higher priority of main thread to preempt job2                      */</span></div>
<div class="line">  <a class="code" href="group___c_m_s_i_s___r_t_o_s___kernel_ctrl.html#ga53d078a801022e202e8115c083ece68e" title="Initialize the RTOS Kernel for creating objects.">osKernelInitialize</a> ();</div>
<div class="line">   </div>
<div class="line">  thread1_id = <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gac59b5713cb083702dce759c73fd90dff" title="Create a thread and add it to Active Threads and set it to state READY.">osThreadCreate</a>(<a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453" title="Access a Thread definition.">osThread</a>(job1),NULL);  <span class="comment">/* create thread1       */</span></div>
<div class="line">  thread2_id = <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gac59b5713cb083702dce759c73fd90dff" title="Create a thread and add it to Active Threads and set it to state READY.">osThreadCreate</a>(<a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453" title="Access a Thread definition.">osThread</a>(job2),NULL);  <span class="comment">/* create thread2       */</span></div>
<div class="line">  thread3_id = <a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gac59b5713cb083702dce759c73fd90dff" title="Create a thread and add it to Active Threads and set it to state READY.">osThreadCreate</a>(<a class="code" href="group___c_m_s_i_s___r_t_o_s___thread_mgmt.html#gaf0c7c6b5e09f8be198312144b5c9e453" title="Access a Thread definition.">osThread</a>(job3),NULL);  <span class="comment">/* create thread3       */</span></div>
<div class="line"> </div>
<div class="line">  <a class="code" href="group___c_m_s_i_s___r_t_o_s___kernel_ctrl.html#gaab668ffd2ea76bb0a77ab0ab385eaef2" title="Start the RTOS Kernel.">osKernelStart</a> ();</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Mon Feb 24 2014 11:07:07 for CMSIS-RTOS RTX by ARM Ltd. All rights reserved.
	<!--
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.2 
	-->
	</li>
  </ul>
</div>
</body>
</html>
